package com.longjianghuasheng.logistics.business.freight

import android.os.Bundle
import android.view.View
import coil.load
import com.longjianghuasheng.logistics.R
import com.longjianghuasheng.logistics.base.BaseFragment
import com.longjianghuasheng.logistics.databinding.FragmentPersonalCooperationBinding
import com.longjianghuasheng.logistics.eventViewModel
import com.longjianghuasheng.logistics.ext.observer
import com.longjianghuasheng.logistics.ext.onClickIsLogin
import com.longjianghuasheng.logistics.ext.selectPicture
import com.longjianghuasheng.logistics.ext.showPicker
import me.hgj.jetpackmvvm.ext.view.invisible
import me.hgj.jetpackmvvm.ext.view.visible

//个人合作
class PersonalCooperationFragment :
    BaseFragment<PersonalCooperationViewModel, FragmentPersonalCooperationBinding>() {

    private val cardFrontListener = { _: View ->
        selectPicture {

            mViewModel.card_front = it.realPath

            mDatabind.ivCardFront.run {
                visible()
                load(it.path)
            }


        }

    }
    private val cardBackListener = { _: View ->
        selectPicture {

            mViewModel.card_back = it.realPath

            mDatabind.ivCardBack.run {
                visible()
                load(it.path)

            }


        }

    }


    override fun layoutId(): Int = R.layout.fragment_personal_cooperation

    override fun initView(savedInstanceState: Bundle?) {

        mDatabind.initListener()
    }

    fun FragmentPersonalCooperationBinding.initListener() {


        ivCardFront.setOnClickListener(cardFrontListener)
        ivClickLeft.setOnClickListener(cardFrontListener)
        tvDriverLicensePositive.setOnClickListener(cardFrontListener)

        ivCardBack.setOnClickListener(cardBackListener)
        ivClickRight.setOnClickListener(cardBackListener)
        tvDriverLicenseBack.setOnClickListener(cardBackListener)


        stvSex.setOnClickListener {

            showPicker(options = arrayOf("男", "女")) {

                mViewModel.sex = if (it == "男") 1 else 2

                stvSex.setCenterString(it)
            }

        }


        btnCommit.onClickIsLogin {

            mViewModel.run {


                names = stvName.centerEditValue
                mobile = stvPhone.centerEditValue
                card_no = stvIdCard.centerEditValue

                people_work()
            }
        }

    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.notify.observer(this) {
            mDatabind.clear()
            eventViewModel.onGoBackToMyEvent.value = 0
        }
    }

    fun FragmentPersonalCooperationBinding.clear() {

        stvName.setCenterEditString("")
        stvPhone.setCenterEditString("")
        stvIdCard.setCenterEditString("")

        stvSex.setCenterString("请选择性别")

        ivCardFront.invisible()
        ivCardBack.invisible()
        mViewModel.run {
            card_front = ""
            card_back = ""
            sex = 0
        }
    }

    override fun initData() {
        super.initData()
    }

    override fun lazyLoadData() {
        super.lazyLoadData()
    }

}